package com.gitee.wsl.ext.bitcode



fun Int.bitCount(): Int {
    // HD, Figure 5-2
    var i = this
    i -= ((i ushr 1) and 0x55555555)
    i = (i and 0x33333333) + ((i ushr 2) and 0x33333333)
    i = (i + (i ushr 4)) and 0x0f0f0f0f
    i += (i ushr 8)
    i += (i ushr 16)
    return i and 0x3f
}

/**
 * Returns the number of one-bits in the two's complement binary
 * representation of the specified `long` value.  This function is
 * sometimes referred to as the *population count*.
 *
 * @param i the value whose bits are to be counted
 * @return the number of one-bits in the two's complement binary
 * representation of the specified `long` value.
 * @since 1.5
 */
fun Long.bitCount(): Int {
    // HD, Figure 5-2
    var i = this
    i -= ((i ushr 1) and 0x5555555555555555L)
    i = (i and 0x3333333333333333L) + ((i ushr 2) and 0x3333333333333333L)
    i = (i + (i ushr 4)) and 0x0f0f0f0f0f0f0f0fL
    i += (i ushr 8)
    i += (i ushr 16)
    i += (i ushr 32)
    return i.toInt() and 0x7f
}